//doge管理总节点
cc.Class({
    extends: cc.Component,

    properties: {
        //doge每次的生成数量
        doge_count: 0,
        //doge的最小速度
        doge_speedmin: 0,
        //doge的最大速度
        doge_speedmax: 0,
        //doge预制体对象
        doge_prefab: {
            default: [],
            type: cc.Prefab
        },
        //狗狗对象池
        doge_pool: {
            default: null,
            visible: false,
        },
        //位于当前的所有子节点
        array: {
            default: null,
            visible: false
        }
    },

    onLoad () {
        this.interval = 0;
        this.init_pool();
        this.create_doge(2);
        this.array = this.node.children;
        //定时器
        this.schedule(this.timeout,0.8);
    },

    update (dt) {
        this.interval += dt;
        if(parseFloat(this.interval.toFixed(2)) >= 0.5) {
            this.interval = 0;
            this.check_doge();
        }
    },

    //
    timeout () {
        if(Game.State == GameState.End) {return;}
        this.array = this.node.children;  //获取子节点
        //cc.log(this.array.length);
        this.total_move(this.array);
    },

    //检测doge挂掉状态
    check_doge () {
        var doge = null;
        for (var i = 0; i < this.array.length; i++) {
            doge = this.array[i].getComponent("doge_self");
            if(doge.doge_action == "die") {
                this.receive_node(this.array[i]);
            }
        }
    },

    //创建doge初始化对象
    doge_init (ac,ar,de) {
        var init_doge = {
            action: ac || "walk", 
            arrive: ar || cc.p(0,0), 
            deviation: de || {angel:30,dir:"LT"}
        }
        return init_doge;
    },

    //翻转doge的朝向 [dir:方向 node:要翻转的节点]
    flip_doge (dir,node) {
        switch(dir) {
            case "LT":
            case "LB":
                node.scaleX = 1;
            break;
            case "RT":
            case "RB":
                node.scaleX = -1;
            break;
        }
    },

    //初始化创建方法
    //node:创建的节点 action:动作类型 arrive:到达位置
    init_create(node,action,arrive) {
        var speed = 0;
        var pos_arr = cc.p(0,0);
        var doge_obj = null;
        var random = cc.p(0,0);
        var deviation = null;
        var doge = node.getComponent("doge_self");
        pos_arr.x = parseFloat(arrive.x.toFixed(2));
        pos_arr.y = parseFloat(arrive.y.toFixed(2));
        random = this.create_position(Game.Screen);
        node.x = random.x;
        node.y = random.y;
        deviation = this.move_deviation(node,pos_arr);
        speed = Commond.Random_Section(this.doge_speedmin,this.doge_speedmax);  //设置随机速度
        doge_obj = this.doge_init(action,pos_arr,deviation);
        doge.init_state(doge_obj);
        doge.modfilys_node(action,doge.doge_type);
        doge.doge_speed = speed;
    },

    //初始化对象池
    init_pool () {
        //预制体对象总类数
        var sum_prefab = this.doge_prefab.length;
        //初始化对象池总数
        var total_count = this.doge_count;
        this.doge_pool = new cc.NodePool();
        for(var i = 0; i < total_count; i++) {
            for(var j = 0; j < sum_prefab; j++) {
                var doge = cc.instantiate(this.doge_prefab[j]);
                this.doge_pool.put(doge); //放入对象池
            }
        }
    },

    //回收对象
    receive_node(node){
        var script_doge = node.getComponent("doge_self");
        //若当前的doge节点的状态是挂了则回收这个节点
        if(script_doge.doge_action == "die") {
            this.doge_pool.put(node);
            if(this.array.length >= 20) {return}  //若当前子节点到达20个则停止生成doge
            this.create_doge(2);  //每回收一次节点就再重新创建两个doge对象
        }
    },

    //获取运动角度
    move_deviation (posA,posB) {
        var angel = 0;
        var deviation = {angel:0,dir:""}
        var slope = (posA.y - posB.y) / (posA.x - posB.x); //计算斜率
        slope = Math.abs(parseFloat(slope.toFixed(2)));
        deviation.dir = Commond.Return_Direction(posA,posB);
        angel = parseFloat(Math.atan(slope).toFixed(2));  //利用反正切计算角度
        deviation.angel = Commond.Transform_Radius(angel);
        return deviation;
    },

    //从对象池请求对象
    //count:请求数量
    create_doge (count) {
        var doge = null;
        var parent = this.node;
        var size = this.doge_pool.size(); //当前对象池空余对象数量
        //判断是否足够创建当前一个或多个对象
        if(size >= count) {
            for(var i = 0; i < count; i++) {
                //设置随机速度
                doge = this.doge_pool.get();
                this.init_create(doge,"walk",Game.Cat.getPosition());
                parent.addChild(doge);  //加入当前父节点中
            }
        }else {
            var length = this.doge_prefab.length;
            for(var j = 0; j < length; j++) {
                doge = cc.instantiate(this.doge_prefab[j]);
                this.init_create(doge,"walk",Game.Cat.getPosition());
                parent.addChild(doge);
            }
        }
    },

    //随机创建狗狗位置
    //screen:屏幕对象
    create_position (screen) {
        var random = cc.p(0,0);
        var width = screen.width;
        var height = screen.height;
        var anchor = screen.anchor;
        var boundary = {x_min:0, x_max:0, y_min:0, y_max:0}  //边界对象
        boundary.x_min = -1 * (width * anchor.x) + 50;
        boundary.x_max = (width * anchor.x) - 50;
        boundary.y_min = -1 * (height * anchor.y) + 50;
        boundary.y_max = (height * anchor.y) - 50;
        random.x = Commond.Random_Section(boundary.x_min,boundary.x_max);
        random.y = Commond.Random_Section(boundary.y_min,boundary.y_max);
        return random;
    },

    //狗狗运动总管理
    //array:节点数组
    total_move (array) {
        var deviation = null;
        var doge_obj = null;
        var doge_script = null;
        var cat = Game.Cat.getPosition();  //获取当前猫猫位置
        var doge_action = "";
        if(array.length == 0) {return}
        for(var i = 0; i < array.length; i++) {
            doge_script = array[i].getComponent("doge_self");
            doge_action = doge_script.doge_action;
            switch(doge_action){
                //定时更新与猫猫的位置
                case "walk":
                    deviation = this.move_deviation(array[i],cat);
                    this.flip_doge(deviation.dir,array[i]);
                    doge_obj = this.doge_init("walk",cat,deviation);
                    doge_script.init_state(doge_obj);
                break;
            }
        }
    },

    //重置所有doge
    repeat_doge () {
        var arr = this.node.children;
        for(var i = 0; i < arr.length; i++) {
            //将所有节点回收进对象池
            this.receive_node(arr[i]);
            //销毁节点
            arr[i].destroy();
        }
        this.init_pool();
        this.create_doge(2);
    }
});
